package com.piao.springboot_scheduled_jsoup.Service;

import com.piao.springboot_scheduled_jsoup.Entity.FoodBean;
import com.piao.springboot_scheduled_jsoup.Entity.FoodStepsBean;
import com.piao.springboot_scheduled_jsoup.Entity.HotSearch;
import com.piao.springboot_scheduled_jsoup.mapper.HotSearchMapper;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class MyJSoupService {
    List<FoodBean> mList;
    @Resource
    HotSearchMapper hotSearchMapper;

    //定时爬取微博热搜 每天中午12点爬
    @Scheduled(cron ="0 52 22 * * *")
    public void CrawlingHotSearch() {
        try {
            String urlStr = "https://s.weibo.com/top/summary";
            final Document doc = Jsoup.connect(urlStr).get();
            Elements trs = doc.select("tbody").select("tr");
            for (int i = 1; i < trs.size(); i++) {
                Elements tds = trs.get(i).select("td");
                String rank = tds.get(0).text();//排名
                String num = tds.get(1).select("span").text();//热度指数
                String title = tds.get(1).select("a").text();//标题
                String url = tds.get(1).select("a").attr("href");//热搜详细（标题+热度）
                String baseurl="https://s.weibo.com";//基址
                //时间
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
                //id
                String hotSearchId= UUID.randomUUID()+"";
                //以 id+时间+排名+热搜标题+热搜指数+有效URL的形式输出
                System.out.println("id:"+hotSearchId+" 时间"+date+" 排名"+rank+" 标题"+title+" 热搜指数"+num+" 有效URL"+baseurl+url);
                hotSearchMapper.insert(new HotSearch(hotSearchId,date,rank,title,num,baseurl+url));//插入单条热搜数据
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //定时爬取菜品 概要信息
//    @Scheduled
    public void CrawlingFood() {



    }


    //定时爬取菜品
    public void CrawlingFoodDetail(int position) {

        try {
            Thread.sleep(300);
            //首先获取被点击的item的三个属性（教程网站不需直接用，后面用于获取步骤信息时再用）

            String picUrl = mList.get(position).getPicUrl();//菜的图片url
            String courseUrl = mList.get(position).getCourseUrl();//教程网站
            String foodName = mList.get(position).getFoodName();//菜名
            String foodMaterials = mList.get(position).getFoodMaterials();//食材
            String[] foodInforArr = new String[4];//菜品4个信息：口味 工艺 耗时 难度
            List<String> materialQuantityList = new ArrayList<>();//食材数量不同
            List<String> stepList = new ArrayList<>();//做法步骤
            List<String> stepPicList = new ArrayList<>();//做法步骤对应图片

            Document document = Jsoup.connect(courseUrl).get();
            //Element introduce = document.getElementById("block_txt1");//有的没有介绍，批量处理会报空指针错误，暂时不加
            Elements foodInfor = document.getElementsByClass("category_s1");
            Elements materialInfor = document.getElementsByClass("category_s2");
            Elements step = document.getElementsByClass("recipeStep_word");
            Elements stepPic = document.getElementsByClass("recipeStep_img");
            //食材种类数
            int num = materialInfor.size() - 4;//与网站HTML标记相关，前四个为菜品信息
            //Log.d("FoodGodS调式", "" + num);//调试是否符和预期

            //菜品的4个信息
            //Log.d("FoodGodS调式", "菜品的4个信息口味工艺耗时难度:");
            for (int i = 0; i < 4; i++) {
                foodInforArr[i] = foodInfor.get(num + i).select("a").attr("title");
                //Log.d("FoodGodS调式", "" + foodInfor.get(num + i).select("a").attr("title"));
            }

            //原材料的数量信息
            for (int i = 0; i < num; i++) {
                materialQuantityList.add("" + materialInfor.get(i).text());
                //Log.d("FoodGodS调式", "" + materialInfor.get(i).text());
            }

            //步骤(文字)
            for (int i = 0; i < step.size(); i++) {
                stepList.add("" + step.get(i).text());
                // Log.d("FoodGodS调式", "" + step.get(i).text());
            }
            //步骤(图片)
            for (int i = 0; i < stepPic.size(); i++) {
                stepPicList.add("" + stepPic.get(i).select("img").attr("src"));
                //Log.d("FoodGodS调式", "" + stepPic.get(i).select("img").attr("src"));
            }

            //将装备好数据放入
            FoodStepsBean foodStepsBean = new FoodStepsBean(picUrl, foodName, foodMaterials, courseUrl,
                    foodInforArr, materialQuantityList, stepList, stepPicList);
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }

    }//public void CrawlingFoodDetail()




}
